
import hljs from 'highlight.js/lib/core'
import hljsVuePlugin from '@highlightjs/vue-plugin'

// 导入所需语言
import java from 'highlight.js/lib/languages/java'
import javascript from 'highlight.js/lib/languages/javascript'
import typescript from 'highlight.js/lib/languages/typescript'
import xml from 'highlight.js/lib/languages/xml'
import sql from 'highlight.js/lib/languages/sql'

// 注册语言
hljs.registerLanguage('java', java)
hljs.registerLanguage('javascript', javascript)
hljs.registerLanguage('typescript', typescript)
hljs.registerLanguage('xml', xml)
hljs.registerLanguage('sql', sql)

// 当前加载的highlight.js主题名称
let currentTheme: string | null = null

/**
 * 动态加载highlight.js主题样式
 * @param theme 当前主题名称 (light 或 dark)
 */
export const loadHighlightTheme = async (theme: string): Promise<void> => {
  // 如果主题未改变，则不需要重新加载
  if (currentTheme === theme) {
    return
  }
  
  try {
    // 动态导入对应主题的样式文件
    if (theme === 'dark') {
      await import('highlight.js/styles/atom-one-dark.css')
    } else {
      await import('highlight.js/styles/stackoverflow-light.css')
    }
    
    // 更新当前主题
    currentTheme = theme
  } catch (error) {
    console.error('Failed to load highlight.js theme:', error)
  }
}

// 导出配置好的插件、hljs实例和主题加载函数
export { hljs, hljsVuePlugin }